<template>
  <div class="orrOutcupStatistics-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="选择日期" prop="businessDate">
          <div style="display: flex; align-items: center">
            <el-button-group style="display: flex; align-items: center">
              <el-button type="primary">当天</el-button>
              <el-button>其他日期</el-button>
            </el-button-group>
            <el-input
              style="margin-left: 6px"
              placeholder="请输入选择日期"
              v-model="searchForm.businessDate"
              autocomplete="off"
            />
          </div>
        </el-form-item>
        <el-form-item label="门店组" prop="storeNumber">
          <el-input
            placeholder="请输入门店组"
            v-model="searchForm.storeNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input
            placeholder="请输入门店名称"
            v-model="searchForm.storeName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
            <el-button @click="startClear" type="success"> 导出报表 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="storeNumber" label="门店编号" width="140" />
          <el-table-column prop="storeName" label="门店名称" width="140" />
          <el-table-column prop="businessDate" label="日期" width="140" />
          <el-table-column prop="orderSummary" label="订单总数" width="120" />

          <el-table-column prop="quantity" label="总生产饮品数" width="130" />
          <el-table-column
            prop="successCount"
            label="正常制作饮品数"
            width="130"
          />
          <el-table-column
            prop="abnormalCount"
            label="异常制作饮品数"
            width="130"
          />
          <el-table-column
            prop="cancelCount"
            label="取消制作饮品数"
            width="130"
          />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <OrrOutcupStatisticsAdd v-model="addUpdateRef"></OrrOutcupStatisticsAdd>
    <OrrOutcupStatisticsRemove v-model="removeRef"></OrrOutcupStatisticsRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getOrrOutcupStatisticsPagination,
  getOrrOutcupStatisticsAllList,
} from '@/api/orr-outcup-statistics.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import OrrOutcupStatisticsRemove from './OrrOutcupStatisticsRemove.vue'
import OrrOutcupStatisticsAdd from './OrrOutcupStatisticsAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `出杯统计报表表`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `出杯统计报表表`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      storeName: '',
      storeNumber: '',
      orderSummary: '',
      quantity: '',
      successCount: '',
      abnormalCount: '',
      businessDate: '',
      cancelCount: '',
      failureCount: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    storeName: item.storeName,
    storeNumber: item.storeNumber,
    orderSummary: item.orderSummary,
    quantity: item.quantity,
    successCount: item.successCount,
    abnormalCount: item.abnormalCount,
    businessDate: item.businessDate,
    cancelCount: item.cancelCount,
    failureCount: item.failureCount,
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `出杯统计报表表`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  storeName: '',
  storeNumber: '',
  orderSummary: '',
  quantity: '',
  successCount: '',
  abnormalCount: '',
  businessDate: '',
  cancelCount: '',
  failureCount: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let storeName = searchForm.value.storeName
  if (storeName) {
    searchCondition.storeName = storeName
  }
  let storeNumber = searchForm.value.storeNumber
  if (storeNumber) {
    searchCondition.storeNumber = storeNumber
  }
  let orderSummary = searchForm.value.orderSummary
  if (orderSummary) {
    searchCondition.orderSummary = orderSummary
  }
  let quantity = searchForm.value.quantity
  if (quantity) {
    searchCondition.quantity = quantity
  }
  let successCount = searchForm.value.successCount
  if (successCount) {
    searchCondition.successCount = successCount
  }
  let abnormalCount = searchForm.value.abnormalCount
  if (abnormalCount) {
    searchCondition.abnormalCount = abnormalCount
  }
  let businessDate = searchForm.value.businessDate
  if (businessDate) {
    searchCondition.businessDate = businessDate
  }
  let cancelCount = searchForm.value.cancelCount
  if (cancelCount) {
    searchCondition.cancelCount = cancelCount
  }
  let failureCount = searchForm.value.failureCount
  if (failureCount) {
    searchCondition.failureCount = failureCount
  }

  tableLoading.value = true
  getOrrOutcupStatisticsPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.orrOutcupStatistics-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.orrOutcupStatistics-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
